package sw6.girafplaceclient;

import java.util.ArrayList;
import java.util.List;

import girafPlace.lib.*;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ApplicationAdapter extends BaseAdapter {
	
	protected List<Application> apps = new ArrayList<Application>();
	protected static Context mContext;
	protected static String tag = "GirafPlaceClient";

	/**
	 * @return the apps
	 */
	public List<Application> getApps() {
		return apps;
	}

	/**
	 * @param apps the apps to set
	 */
	public void setApps(List<Application> apps) {
		this.apps = apps;
		this.notifyDataSetChanged();
	}

	private static ApplicationAdapter instance = null;
	private ApplicationAdapter() {
		// Exists only to defeat instantiation.
	}
	public static ApplicationAdapter getInstance(Context context) {
		mContext = context;
		if(instance == null) {
			Log.d(tag,"Creating the first instance and executing the network task.");
			instance = new ApplicationAdapter();
			new NetworkTask(mContext).execute();
		}
		return instance;
	}
	   
	@Override
	public int getCount() {
		return apps.size();
	}

	@Override
	public Object getItem(int position) {
		return apps.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppView appView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            appView = new AppView(mContext,apps.get(position));        
        } else {
            appView = (AppView) convertView;
        }
        
        appView.setApp(apps.get(position));
        
        return appView;
	}
}